package second.demo2;

import java.lang.reflect.Field;

/**
 * @Description:
 * @author:LiWei
 * @date:2022年10月27日12:28
 */
public class Test {
    public static void main(String[] args) throws Exception {
        Class<Student> studentClass= Student.class;
        Student student = studentClass.newInstance();
        System.out.println(student);

        Field name = studentClass.getDeclaredField("name");
        name.set(student,"aaa");
        System.out.println(student);

        //通过反射获取指定属性值
        Object o = name.get(student);
        System.out.println(o);

        Field age = studentClass.getDeclaredField("age");
        //允许访问私有的
        age.setAccessible(true);
        age.set(student,22);
        System.out.println(student);

        Object o1 = age.get(student);
        System.out.println(o1);
    }
}

class People{
    public String address;
    private String sex;

}

class Student extends People {
    public String name;
    private int age;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}